package demo;

import java.sql.*;

/**
 * 数据库操作
 * Created by yezhengle on 2017/7/29.
 * 1)CLASS.forName()
 * 2)DriverManager.getConnection()
 * 3)con.createStatement()
 * 4)statement.executeQuery\excuteUpdate
 * 5)close
 */
public class E_19 {
    private static final String CLASS_NAME = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String NAME = "root";
    private static final String PASSWORD = "123456";
    public static void main(String[] args){
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try{
            Class.forName(CLASS_NAME);
            try {
                connection = DriverManager.getConnection(URL, NAME, PASSWORD);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from demo");
            int loginTime = 0;
            while (resultSet.next()){
                System.out.println(resultSet.getString("id"));
                loginTime = resultSet.getInt("login_time");
                loginTime++;
            }

            statement.executeUpdate("update demo set name = '11' ,login_time='"+loginTime+"' where id = 19 ");

            resultSet = statement.executeQuery("select * from demo");
            while (resultSet.next()){
                System.out.println(resultSet.getString("id")+" "+resultSet.getString("name")+" "+resultSet.getInt("login_time"));
            }
        }catch (ClassNotFoundException |SQLException  e){
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
